package xyz.feiwo.singleton;

/**
 * Created by fei wo at 2021/4/21.
 *
 * @author fei wo <a mailto:feiwodev@foxmail.com>feiwodev@foxmail.com</a>
 * @since 1.0
 *
 * 懒汉式单例模式， 在获取实例对象的时候创建对象
 */
public class LazyTypeSingleton {

    private static volatile LazyTypeSingleton instance; // 防止指令重排序

    private LazyTypeSingleton(){}


    public static LazyTypeSingleton getInstance() {
        // DCL
        if ( instance == null ) {
            synchronized (LazyTypeSingleton.class) {
                if ( instance == null ) {
                    instance = new LazyTypeSingleton();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        LazyTypeSingleton singleton01 = LazyTypeSingleton.getInstance();
        LazyTypeSingleton singleton02 = LazyTypeSingleton.getInstance();
        System.out.println(singleton01 == singleton02);
    }
}
